﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" Satuan to change the class name "SatuanServices" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class SatuanServices : ISatuanServices
    {
        private readonly SatuanRepository SatuanRepository;

        public SatuanServices()
        {
            SatuanRepository = new SatuanRepository(new WcfSessionStorage());
        }

        #region ISatuanServices Members

        public List<SatuanDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                       string sortType = "ASC")
        {
            var Satuans = new List<Satuan>();
            var Results = new List<SatuanDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Satuans = SatuanRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Satuans = SatuanRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = SatuanRepository.RecordCount;

            Satuans.ForEach(x =>
                                {
                                    var SatuanDto = new SatuanDTO();
                                    Mapper.Map(x, SatuanDto);
                                    //AutoMapper.Mapper.Map(x.ParentSatuan, SatuanDto.ParentSatuan);
                                    Results.Add(SatuanDto);
                                }
                );

            return Results;
        }

        public bool Save(SatuanDTO SatuanDTO)
        {
            bool res;
            try
            {
                Satuan Satuan = SatuanRepository.GetDataOne(x => x.KodeSatuan == SatuanDTO.KodeSatuan);
                var user = new User();
                if (Satuan == null)
                {
                    Satuan = new Satuan();
                    SatuanDTO.created_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(SatuanDTO.created_by, user);
                    //SatuanDTO.modified_by = null;
                }
                else
                {
                    SatuanDTO.modified_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(SatuanDTO.modified_by, user);
                }
                Mapper.Map(SatuanDTO, Satuan);
                SatuanRepository.SaveOrUpdate(Satuan);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public bool Delete(SatuanDTO SatuanDto)
        {
            bool res;
            try
            {
                var Satuan = new Satuan();
                Mapper.Map(SatuanDto, Satuan);
                SatuanRepository.Delete(Satuan);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public List<SatuanDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                 out long recordCount)
        {
            var Satuans = new List<Satuan>();
            var Results = new List<SatuanDTO>();
            Satuans = SatuanRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = SatuanRepository.RecordCount;
            Satuans.ForEach(x =>
                                {
                                    var SatuanDto = new SatuanDTO();
                                    Mapper.Map(x, SatuanDto);
                                    Results.Add(SatuanDto);
                                }
                );

            return Results;
        }


        public List<SatuanDTO> GetAllSatuan()
        {
            List<Satuan> Satuans = SatuanRepository.FetchAll().ToList();
            var Results = new List<SatuanDTO>();
            Satuans.ForEach(x =>
                                {
                                    var SatuanDto = new SatuanDTO();
                                    Mapper.Map(x, SatuanDto);
                                    Results.Add(SatuanDto);
                                }
                );

            return Results;
        }


        public SatuanDTO GetSatuan(string kodeSatuan)
        {
            Satuan Satuan = SatuanRepository.GetDataOne(x => x.KodeSatuan == kodeSatuan);
            var SatuanDto = new SatuanDTO();
            Mapper.Map(Satuan, SatuanDto);
            return SatuanDto;
        }

        #endregion
    }
}